import { getUrlParam } from './index';

// 将null或者NaN转换为'—'
export const transNullTarget = (target: any, fixed: boolean = false) => {
  if (!target || `${target}` === 'NaN') {
    return '—';
  }
  if (fixed) {
    return (+target).toFixed(2);
  }
  return target;
};

// 从地址栏获取数据应用自动排序
export const applyAutoSort = (table: any, handleSortChange: any) => {
  const sortBy = getUrlParam('sortBy');
  const sortOrder = getUrlParam('sortOrder');

  if (sortBy && sortOrder) {
    // 设置降序排序
    const columnIndex = table?.columns?.findIndex(
      (item: any) => item.property === sortBy,
    );
    const icon = table?.$el?.querySelectorAll(`th i.${sortOrder}`)?.[
      columnIndex
    ];

    if (icon) {
      icon.click();
    }
    handleSortChange({ prop: sortBy, order: sortOrder });
  }
};
